﻿using System;
using System.Collections.Generic;
using System.Linq;
using Quicklight.Web.Injection;

namespace Quicklight.Web.Regions {

   public class RegionManager {
      private string name;
      private List<Region> regions;

      public RegionManager(string name) {
         this.name = name;
         this.regions = new List<Region>();
      }

      public string Name {
         get {
            return name;
         }
         set {
            name = value;
         }
      }

      public static RegionManager DefaultManager {
         get { return GetManager(""); }
      }

      public static RegionManager GetManager(string name) {
         if (!ObjectManager.Instance.IsInstanceRegistered<RegionManager>(name))
            ObjectManager.Instance.RegisterInstance<RegionManager>(name, new RegionManager(name));

         return ObjectManager.Instance.Resolve<RegionManager>(name);
      }

      public Region GetRegion(string regionName) {
         return regions.Where(r => r.Name == regionName).FirstOrDefault();
      }

      public Region AddRegion(string regionName) {
         Region region = GetRegion(regionName);

         if (region == null) {
            region = new Region(regionName, this);
            regions.Add(region);
         }

         return region;
      }

      public void AddParameters(string regionName, string parameterName, object parameterValue) {
         Region region = regions.Where(r => r.Name == regionName).FirstOrDefault();

         if (region == null) {
            region = new Region(regionName, this);
            regions.Add(region);
         }

         region.Parameters.Add(parameterName, parameterValue);

      }

      public void LoadInRegion(string regionName, IViewable view) {
         Region region = regions.Where(r => r.Name == regionName).FirstOrDefault();

         if (region == null) {
            region = new Region(regionName, this);
            regions.Add(region);
         }

         region.Load(view);
      }

      public void LoadInRegion<TViewable>(string regionName) where TViewable : IViewable {
         Region region = regions.Where(r => r.Name == regionName).FirstOrDefault();

         if (region == null) {
            region = new Region(regionName, this);
            regions.Add(region);
         }

         region.Load<TViewable>();
      }

      public void LoadInRegion<TViewable>(string regionName, string viewName) where TViewable : IViewable {
         Region region = regions.Where(r => r.Name == regionName).FirstOrDefault();

         if (region == null) {
            region = new Region(regionName, this);
            regions.Add(region);
         }

         region.Load<TViewable>(viewName);
      }

      public void ShowInRegion(string regionName, IViewable view) {
         Region region = regions.Where(r => r.Name == regionName).FirstOrDefault();

         if (region == null) {
            region = new Region(regionName, this);
            regions.Add(region);
         }

         region.Show(view);
      }

      public void ShowInRegion<TViewable>(string regionName) where TViewable : IViewable {
         Region region = regions.Where(r => r.Name == regionName).FirstOrDefault();

         if (region == null) {
            region = new Region(regionName, this);
            regions.Add(region);
         }

         region.Show<TViewable>();
      }

      public void ShowInRegion<TViewable>(string regionName, string viewName) where TViewable : IViewable {
         Region region = regions.Where(r => r.Name == regionName).FirstOrDefault();

         if (region == null) {
            region = new Region(regionName, this);
            regions.Add(region);
         }

         region.Show<TViewable>(viewName);
      }

   }
}
